<?php
/**
 * Created by PhpStorm.
 * User: wujun
 * Date: 2017/10/20
 * Time: 下午1:02
 */


include './wechat.class.php';

class Wx
{
    /*
    // test
    private $option = [
        'token' => '75d16409c88da995685b22792327a308', //填写你设定的key
        'encodingaeskey' => 'Se7tyC7SX27JmQyJbeK8Nlqy2YZYdfE7GE1Pdjg0Ptl', //填写加密用的EncodingAESKey
        'appid' => 'wx9a9042bbba02c251', //填写高级调用功能的app id
        'appsecret' => 'fc1a233c85435e2577e27d04855a1bc7' //填写高级调用功能的密钥
    ];
    */


    // tuniao
    private $option = [
        'token' => 'eTlre9R175DLNnbg8owJm1FHiYSSUJWN', //填写你设定的key
        'encodingaeskey' => 'U7iTD5hIVZlLwS1eDQh82plgge4Prb2FkYTDvSRajEm', //填写加密用的EncodingAESKey
        'appid' => 'wx0774ba4f6f80ec86', //填写高级调用功能的app id
        'appsecret' => '168bf2e6ecddf7a17dc835793b0efc7a' //填写高级调用功能的密钥
    ];


    /*
        // lycode
        private $option = [
            'token' => 'eTlre9R175DLNnbg8owJm1FHiYSSUxIN', //填写你设定的key
            'encodingaeskey' => 'yuRBglEsuQOPg0LHMY7IalKzrLkxhscvDOj3Vuknq9k', //填写加密用的EncodingAESKey
            'appid' => 'wx776619a6460364a2', //填写高级调用功能的app id
            'appsecret' => '819ceb99db03c7de595eff52572878d7' //填写高级调用功能的密钥
        ];
        */


    private $openId;
    private $revType;

    private $weObj = null;

    public function __construct()
    {
        $this->_log(__FUNCTION__);
        if ($this->weObj == null) {
            $this->weObj = new Wechat($this->option);
        }
    }

    private function _log($msg)
    {
//        $f = fopen('./1.log', 'a+');
//        fwrite($f, $msg . "\r\n");
//        fclose($f);
    }


    public function doEvent()
    {
        $this->_log(__FUNCTION__);
        if (isset($_GET['echostr'])) {
            $this->weObj->valid();
            exit();
        }

        $this->openId = $this->weObj->getRev()->getRevFrom();
        $this->_log($this->openId);
        $this->revType = $this->weObj->getRev()->getRevType();
        $this->_log($this->revType);


        switch ($this->revType) {
            case Wechat::MSGTYPE_TEXT :
                // 关键字回复
                $keyword = trim($this->weObj->getRevContent());
                $this->_log('keyword: ' . $keyword);
                $this->_do_keyword($keyword);
                break;

            case Wechat::MSGTYPE_EVENT :
                // 事件
                $event = $this->weObj->getRev()->getRevEvent();
                $this->_log('event: ' . json_encode($event, JSON_UNESCAPED_UNICODE));
                if ($event['event'] == 'subscribe') {
                    // 用户关注事件
                    $text = "欢迎来到途小鸟的世界，在这里将为你打开物流世界的大门！\n想报价？↓↓↓点击底部菜单栏【我要报价】\n看运单？↓↓↓点击底部菜单栏【我的运单】\n有疑问？↓↓↓点击底部菜单栏【我的服务】\n你想要的这里都有，还有不定期的优惠券哟~答应我，三生三世，永不取关。";
                    $this->weObj->text($text)->reply();
                } elseif ($event['event'] == 'unsubscribe') {
                    // 用户取消关注事件
                } elseif ($event['event'] == 'CLICK') {
                    // 点击事件
                    $this->_do_keyword($event['key']);
                }
                break;
            default :
                // $this->weObj->text('sss')->reply();
        }
    }


    //处理关键字
    private function _do_keyword($keyword = '')
    {
        $ruleInfo['TNKEFU_1001'] = [
            'msg_type' => 'text',
            'content' => '请拨打电话 400-835-2166'
        ];

        if (!empty($ruleInfo) && isset($ruleInfo[$keyword])) {
            $this->_do_reply_content($ruleInfo[$keyword]);
        } else {
            // 多客服
            // $this->weObj->transfer_customer_service();
        }
    }

    private function _do_reply_content($info)
    {
        switch ($info['msg_type']) {
            case "text":
                if (isset($info['content']) && $info['content']) {
                    $this->weObj->text($info['content'])->reply();
                }
                break;
            case "article":
                break;
            default:
                //nothing to do;
        }
    }


//    public function createMenu()
//    {
//        $ret_menu ['button'] = array(
//            array(
//                'type' => "click",
//                'name' => '在线客服',
//                'key' => 'TNKEFU_1001'
//            ),
//        );
//        if ($this->weObj->createMenu($ret_menu)) {
//            print_r($this->weObj->getMenu());
//        } else {
//            die('create menu fail');
//        }
//    }

}

//(new Wx())->doEvent();
$wx = new Wx();
$wx->doEvent();
//$wx->createMenu();



